package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class DBUtil 
{
	// Singleton
	private static DBUtil singleton;
	private Connection con;
    private DBUtil(){}
    
	public static DBUtil getInstance()
	{
		if (singleton == null)
			singleton = new DBUtil();
		
		return singleton;
	}	
	public Connection getConnection()
	{
		// Se a conex�o n�o existe ainda
		if (con == null)
		{
			try
			{
				// Carrega a classe do driver JDBC   
				Class.forName("org.postgresql.Driver");   
		    
				// Cria uma conex�o atrav�s do Driver   
				con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/PF2","postgres","post");
			}
			catch(ClassNotFoundException e)
			{
				System.out.println("O driver n�o foi importado");
				e.printStackTrace();
			}
			catch(SQLException e)
			{
				System.out.println("Erro de conex�o com o banco");
				e.printStackTrace();
			}
		}
		
		// Retorna a conex�o
		return con;
	}
}